package com.qeeyou.apps.android.craftsman.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.HelpItemInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2017/12/17.
 */

public class HelpAdapter extends BaseAdapter {

    private Activity mActivity;
    private List<HelpItemInfo> infoList;
    private LayoutInflater mLayoutInflater;

    public HelpAdapter(Activity activity, List<HelpItemInfo> infoList) {
        mActivity = activity;
        mLayoutInflater = LayoutInflater.from(mActivity);
        if(this.infoList == null) {
            this.infoList = new ArrayList<HelpItemInfo>();
        }
        this.infoList.clear();
        if(infoList != null && infoList.size() > 0) {
            this.infoList.addAll(infoList);
        }
    }

    public void setItems(List<HelpItemInfo> infoList) {
        if(this.infoList == null) {
            this.infoList = new ArrayList<HelpItemInfo>();
        }
        this.infoList.clear();
        if(infoList != null && infoList.size() > 0) {
            this.infoList.addAll(infoList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int count = this.infoList != null ? this.infoList.size() : 0;
        return count;
    }

    @Override
    public HelpItemInfo getItem(int i) {
        if(i < 0 || i >= this.infoList.size()) {
            return null;
        }
        return this.infoList.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        if(view == null) {
            view = mLayoutInflater.inflate(R.layout.view_item_help, null, false);
            holder = new ViewHolder();
            holder.txtvTitle = (TextView) view.findViewById(R.id.txtvTitle4Help);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        HelpItemInfo itemInfo = this.infoList.get(position);
        if(itemInfo != null) {
            String titleStr = (position + 1) + "、" + itemInfo.getTitle();
            holder.txtvTitle.setText(titleStr);
        }
        return view;
    }

    static class ViewHolder {
        TextView txtvTitle;
    }
}
